#! /usr/bin/python

from mathex import perm
from bisect import bisect

L = None

def init(bit):
    global L
    L = []
    for i in range(10**(bit-1),10**bit):
        L.append(pow(i,3))
    
def iscube(n):
    ipoint = bisect(L,n)
    return n == L[ipoint-1]

def main():
    for bit in range(4,10):
        init(bit)
        for m in range(10**(bit-1),10**bit):
            print m
            n = m*m*m
            c = set()
            for i in perm(str(n)):
                t = int(''.join(i))
                if iscube(t):
                    c.add(t)
            if len(c) == 5:
                print n
                return
def main1():
    d = {}
    m = 1
    while True:
        n = m**3
        s = list(str(n))
        s.sort()
        s = ''.join(s)
        if d.has_key(s):
            d[s][0] += 1
            d[s][1].append(n)
            if d[s][0] == 5:
                print d[s][1][0]
                return
        else:
            d[s] = [1,[n]]
        m += 1
            
def testmain():
    init(3)
    m = 345
    n = m*m*m
    c = set()
    for i in perm(str(n)):
        t = int(''.join(i))
        if iscube(t):
            print str(t) + ' is cube'
            c.add(t)
    print len(c)

if __name__ == '__main__':
#   testmain()
    main1()
    
